//
//  WHNavigationController.m
//  WHEasyBizLink
//
//  Created by shengmeng chen on 2024/11/23.
//

#import "WHNavigationController.h"



@interface WHNavigationController()<UIGestureRecognizerDelegate>

@end

@implementation WHNavigationController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.navigationBar.translucent  =YES;
    //设置导航的和状态栏
   // self.navigationBar.barTintColor =RGBColor(0, 153,170);
    // 设置右滑手势的代理
    self.interactivePopGestureRecognizer.delegate = self;
    //设置返回颜色为白色
    self.navigationBar.tintColor = [UIColor whiteColor];
    //设置字体为白色
    self.navigationBar.barStyle  = UIBarStyleDefault;
   // self.navigationItem.titleView.backgroundColor = [UIColor whiteColor];
    self.navigationBar.backgroundColor = [UIColor clearColor];

    if (@available(iOS 15.0, *))
    {
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        appearance.backgroundColor = RGBColor(134, 190, 250);
        
        //RGBColor(134,190,250);//[UIColor systemGroupedBackgroundColor];
        appearance.shadowColor     = [UIColor clearColor];
        self.navigationBar.standardAppearance   = appearance;
        self.navigationBar.scrollEdgeAppearance = appearance;
       }
    
    //设置导航字体大小和颜色
    UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
    [self.navigationBar setTitleTextAttributes:
    @{NSFontAttributeName:font,
    NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
}

#pragma  mark - 重写父类的push方法
/**
 *  重写导航控制器的push方法 ： 拦截所有被push进来的子控制器
 *  @param viewController   即将要被push进来的子控制器
 *  @param animated         是否有push动画
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // 重写父类的方法一定要先调用父类的super方法
    [super pushViewController:viewController animated:YES];
    
    if (self.childViewControllers.count > 1) {
        UIButton  *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"Navback"] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(pushReturnClick) forControlEvents:UIControlEventTouchDown];
        [btn sizeToFit];
        btn.frame =CGRectMake(0, 0,40, 40);
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 20)];
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
       
       // UIBarButtonItem * leftBarButtonItem =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Navback"] style:UIBarButtonItemStylePlain target:self action:@selector(pushReturnClick)];
        viewController.navigationItem.leftBarButtonItem = leftItem;
        viewController.hidesBottomBarWhenPushed = YES;
    }
}

- (void)pushReturnClick
{
    if (_nDelegate &&[_nDelegate respondsToSelector:@selector(navigationShouldPopOnpushReturnClick)]) {
        [_nDelegate navigationShouldPopOnpushReturnClick];
    }
    [self popViewControllerAnimated:YES];
    //[[WHDialog sharedManager] closeAllDialogWithDialog:nil];
}

#pragma  mark - 右滑动手势代理
/**
 *  手势识别器对象会调用该方法来决定右滑的手势是否有效
 *
 *  @param gestureRecognizer 右滑的手势
 *
 *  @return 是否有效 YES:有效  NO:无效
 */
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
//    如果子控件的个数为1时，意味着控制器在TabBar控制器上，此时就不需要对右滑的手势进行识别。
//    if (self.childViewControllers.count == 1) {
//        return NO;
//    }
//   右滑手势何时有效: 当导航的自控制器数大于1时就有效。
    return self.childViewControllers.count > 1;
}

@end


